//
// Created by Administrator on 2022/6/14.
//
#include "utiledef.h"
#include "com_device_devicefun.h"
#include "DeviceFun.h"


JNIEXPORT jint JNICALL Java_com_device_DeviceFun_Port(JNIEnv *env, jclass clazz, jstring portstr) {
    // TODO: implement Port()
    const char *file_p = (*env)->GetStringUTFChars(env,portstr, 0);
    Device_Port(file_p);
    return 0;
}


JNIEXPORT jint JNICALL Java_com_device_DeviceFun_Init(JNIEnv *env, jclass clazz) {
    // TODO: implement Init()
    return Device_Init();

}

JNIEXPORT jint JNICALL Java_com_device_DeviceFun_Close(JNIEnv *env, jclass clazz, jint porthandler) {
    // TODO: implement Close()
    return Device_Close(porthandler);

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_DoorCheck(JNIEnv *env, jclass clazz, jint porthandler, jbyte no) {
    // TODO: implement DoorCheck()
    return Device_DoorCheck(porthandler,no);

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_DoorClose(JNIEnv *env, jclass clazz, jint porthandler, jbyte no) {
    // TODO: implement DoorClose()
    return Device_DoorClose(porthandler,no);

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_DoorOpen(JNIEnv *env, jclass clazz, jint porthandler, jbyte no,
                                   jint times) {
    // TODO: implement DoorOpen()
    return Device_DoorOpen(porthandler,no,times);
}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherVersion(JNIEnv *env, jclass clazz, jint porthandler,
                                         jbyteArray buf) {
    // TODO: implement WeigherVersion()
    int result = -1;
    jbyte* jbyte_buf_tmp = (*env)->GetByteArrayElements(env,buf, NULL);
    result= Device_weigher_Getversion(porthandler,jbyte_buf_tmp);
    (*env)->ReleaseByteArrayElements(env, buf,jbyte_buf_tmp,  0);
    return result;
}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherAdjust(JNIEnv *env, jclass clazz, jint porthandler, jbyte no) {
    // TODO: implement WeigherAdjust()
    return Device_WeigherAdjust(porthandler,no);

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherAdjustALL(JNIEnv *env, jclass clazz, jint porthandler) {
    // TODO: implement WeigherAdjustALL()
    return Device_WeigherAdjustALL(porthandler);
}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherCablierate(JNIEnv *env, jclass clazz, jint porthandler, jbyte no,
                                            jint value) {
    // TODO: implement WeigherCablierate()
    return Device_WeigherCablierate(porthandler,no,value );
}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherCablierateALL(JNIEnv *env, jclass clazz, jint porthandler,
                                               jint value) {
    // TODO: implement WeigherCablierateALL()
    return Device_WeigherCablierateALL(porthandler,value);

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherGet(JNIEnv *env, jclass clazz, jint porthandler, jbyte no,
                                     jbyteArray buf) {
    // TODO: implement WeigherGet()
    int result = -1;
    jbyte* jbyte_buf_tmp = (*env)->GetByteArrayElements(env,buf, NULL);
    result=Device_WeigherGet(porthandler,no,jbyte_buf_tmp);
    (*env)->ReleaseByteArrayElements(env,  buf,jbyte_buf_tmp, 0);
    return result;
}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherGetAll(JNIEnv *env, jclass clazz, jint porthandler, jbyte no,
                                        jbyteArray buf) {
    // TODO: implement WeigherGetAll()
    int result = -1;
    jbyte* jbyte_buf_tmp = (*env)->GetByteArrayElements(env,buf, NULL);
    result=Device_WeigherGetAll(porthandler,no,jbyte_buf_tmp);
    (*env)->ReleaseByteArrayElements(env, buf,jbyte_buf_tmp,  0);
    return result;

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherGetParam(JNIEnv *env, jclass clazz, jint porthandler, jbyteArray buf) {
    // TODO: implement WeigherGetParam()
    int result = -1;
    jbyte* jbyte_buf_tmp = (*env)->GetByteArrayElements(env,buf, NULL);
    result=Device_WeigherGetParam(porthandler,jbyte_buf_tmp);
    (*env)->ReleaseByteArrayElements(env,  buf, jbyte_buf_tmp,0);
    return result;

}

JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_WeigherSetParam(JNIEnv *env, jclass clazz, jint porthandler,jbyteArray buf) {
    // TODO: implement WeigherSetParam()
    int result = -1;
    jbyte* jbyte_buf_tmp = (*env)->GetByteArrayElements(env,buf, NULL);
    result=Device_WeigherSetParam(porthandler,jbyte_buf_tmp);
    (*env)->ReleaseByteArrayElements(env, buf,jbyte_buf_tmp,  0);
    return result;

}


JNIEXPORT jint JNICALL
Java_com_device_DeviceFun_UartSendRecv(JNIEnv *env, jclass clazz, jint porthandler, jbyte no,
                                       jint slen, jbyteArray sendbuf, jintArray rlen, jbyteArray recbuf,
                                       jint timeout) {
    // TODO: implement UartSendRecv()
    int result = -1;
    jbyte* jbyte_sendbuf_tmp = (*env)->GetByteArrayElements(env,sendbuf, NULL);

    jint* jint_rlen_tmp = (*env)->GetIntArrayElements(env,rlen,NULL);

    jbyte* jbyte_recbuf_tmp = (*env)->GetByteArrayElements(env,recbuf, NULL);

    result=Device_UartSendRecv(porthandler,no,slen,jbyte_sendbuf_tmp,jint_rlen_tmp,jbyte_recbuf_tmp,timeout);

    (*env)->ReleaseByteArrayElements(env, sendbuf,jbyte_sendbuf_tmp,  0);

    (*env)->ReleaseIntArrayElements(env, rlen,jint_rlen_tmp,  0);

    (*env)->ReleaseByteArrayElements(env, recbuf,jbyte_recbuf_tmp,  0);
    return result;
}



